import { routerMiddleware } from 'connected-react-router'
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import createSagaMiddleware from 'redux-saga'
import rootSaga from 'saga'
import { createBrowserHistory } from 'history'
import { createRootReducer } from '../ducks'

const sagaMiddleware = createSagaMiddleware()

function createStores(history) {
  const middleware = routerMiddleware(history)
  const store = createStore(
    createRootReducer(history),
    composeWithDevTools(applyMiddleware(middleware, sagaMiddleware)),
  )
  sagaMiddleware.run(rootSaga)
  return store
}

export const history = createBrowserHistory()
export const store = createStores(history)
